const { UUIDV4, UUID, STRING, INTEGER, TINYINT, DOUBLE, Model } = require('sequelize');
const db = require('../../helpers/db');
const { checkThePhone } = require('../../helpers/validator');
const OrderItem = require('./order-item');
const AfterSaleRecords = require('./after-sale-records');

class Order extends Model {}

Order.init(
    {
        id: {
            type: UUID,
            defaultValue: UUIDV4,
            primaryKey: true,
            comment: '订单 ID'
        },
        no: {
            type: STRING(24),
            comment: '订单编号'
        },
        user: {
            type: UUID,
            unique: true,
            comment: '外键，用户ID'
        },
        totalPrice: {
            type: DOUBLE,
            comment: '订单总价',
            defaultValue: 0
        },
        postage: {
            type: DOUBLE,
            comment: '邮费',
            defaultValue: 0
        },
        payWay: {
            type: TINYINT(1),
            defaultValue: 0,
            comment: '支付方式：0 支付宝，1微信'
        },
        serialNumber: {
            type: STRING(128),
            comment: '交易流水号'
        },
        logisticsCompany: {
            type: STRING(128),
            comment: '物流公司',
            field: 'logistics_company'
        },
        logisticsNumber: {
            type: STRING,
            comment: '物流单号',
            field: 'logistics_number'
        },
        logisticsStatus: {
            type: TINYINT(1),
            defaultValue: 0,
            comment: '物流状态：0待发货，1已发货，2已完成'
        },
        receiver: {
            type: STRING(16),
            allowNull: false,
            comment: '收货人'
        },
        phone: {
            type: STRING(11),
            unique: true,
            comment: '收货人联系电话',
            allowNull: false,
            validate: {
                customValidator(value) {
                    if (value && !checkThePhone(value)) {
                        throw new Error('请输入正确的手机号码');
                    }
                }
            }
        },
        province: {
            type: STRING(32),
            allowNull: false,
            comment: '收货地址省市信息'
        },
        city: {
            type: STRING(32),
            allowNull: false,
            comment: '收货地址城市信息'
        },
        county: {
            type: STRING(32),
            allowNull: false,
            comment: '收货地址区域信息'
        },
        detailAddress: {
            type: STRING(32),
            comment: '收货地址详细信息',
            allowNull: false,
            field: 'detail_address'
        },
        remark: {
            type: STRING(32),
            comment: '备注'
        },
        status: {
            type: TINYINT(1),
            defaultValue: 0,
            comment:
                '订单状态：0待付款，1待发货，2，待收货，3待评价，4交易成功，5售后中，6售后完成，7交易取消，8删除订单'
        }
    },
    {
        tableName: 'order',
        underscored: true,
        sequelize: db
    }
);

Order.hasMany(OrderItem, {
    sourceKey: 'no',
    foreignKey: 'order'
});

Order.belongsTo(AfterSaleRecords, {
    foreignKey: 'order'
});

module.exports = Order;
